home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / conversion / iffconvert / source.lha / src / iffconvert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-10  |  10.1 KB  |  367 lines

  1. /*************************************************************************
  2. *                                                                        *
  3. *   IFFCONVERT.C                       Modified: 16.4.93                 *
  4. *                                                                        *
  5. *   cc iffconvert.c                                                      *
  6. *                                                                        *
  7. *   ln iffconvert.o getfile.o iffpack.o                                  *
  8. *                                                                        *
  9. *   V1.0                                                                 *
  10. *                                                                        *
  11. *   V1.1   reqtools Filerequester                                        *
  12. *          Topaz 8 fest eingebaut, sonst Probleme mit anderen Fonts      *
  13. *                                                                        *
  14. *   V1.11  Bug mit übergroßen Windows entfernt, nur noch Englische Vers. *
  15. *                                                                        *
  16. *   V1.12  BODY-Chunk jetzt immer auf Wortgröße, 256 Farben              *
  17. *                                                                        *
  18. **************************************************************************/
  19.  
  20. #include <intuition/intuition.h>
  21. #include <intuition/intuitionbase.h>
  22. #include <exec/memory.h>
  23. #include <proto/exec.h>
  24. #include <proto/intuition.h>
  25. #include <proto/graphics.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #define LOAD   0
  31. #define SAVE   1
  32. #define DELETE 2
  33.  
  34. #include "iffmenu.h"
  35.  
  36. #define ERRORSTRINGS
  37.  
  38. #include "iffpack.h"
  39. #include "iffconvert.h"
  40.  
  41.  
  42. #define Trace(s) /* {puts(s);getchar();} */
  43.  
  44. #define SAS_C
  45.  
  46. struct NewScreen ns;
  47. struct NewWindow nw;
  48.  
  49. struct Library *ReqToolsBase;
  50.  
  51. struct IntuitionBase *IntuitionBase=NULL;
  52. struct GfxBase *GfxBase=NULL;
  53. ULONG File;
  54.  
  55. struct Screen *s=NULL;
  56. struct Window *w=NULL;
  57.  
  58. struct TextAttr txtattr= {
  59.    (STRPTR)"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT};
  60.  
  61. FILE *fp=NULL;
  62.  
  63. char *FileRequest();
  64.  
  65. long selected=0;
  66.  
  67. void _abort(void);
  68.  
  69. main()
  70. {
  71.    int err;
  72.    int loaded=0;
  73.    char *name;
  74.    struct IntuiMessage *msg;
  75.    USHORT num;
  76.    ULONG class;
  77.    USHORT code;
  78.  
  79.  
  80.    short ende=0;
  81.  
  82.    new_window.FirstGadget=NULL;
  83.  
  84.    if(!(IntuitionBase = (struct IntuitionBase *)
  85.          OpenLibrary("intuition.library",0L))) quit("No intuition-library");
  86.  
  87.    if(!(GfxBase = (struct GfxBase *)
  88.          OpenLibrary("graphics.library",0L))) quit("No graphics-library");
  89.  
  90.    if(!(ReqToolsBase=(struct Library *)OpenLibrary("reqtools.library",37L)))
  91.       quit("No reqtools-library V37");
  92.  
  93.    if(InitRequest())
  94.       quit("No Memory");
  95.  
  96.    OpenDefaultScreen();
  97.    PrintIText(w->RPort,FIRSTTEXT,0L,0L);
  98.  
  99.    while(!ende) {
  100.       Wait(1L<<w->UserPort->mp_SigBit);
  101.       while(msg=(struct IntuiMessage *)GetMsg(w->UserPort)) {
  102.          class=msg->Class;
  103.          code=msg->Code;
  104.          ReplyMsg((struct Message *)msg);
  105.          if(class==MENUPICK) {
  106.             num=code;
  107.             while(num!=MENUNULL) {
  108.                if(MENUNUM(num)==0){
  109.                   switch(ITEMNUM(num)) {
  110.                      case 0:  if(loaded) {
  111.                                  SetRGB4(&s->ViewPort,0,0,0,0);
  112.                                  SetRGB4(&s->ViewPort,1,15,15,15);
  113.                               }
  114.                               if(name=FileRequest(w,LOAD)) {
  115.                                  if(fp=fopen(name,"r")) {
  116.                                     Trace("LoadScreen");
  117.                                     LoadScreen(fp);
  118.                                     Trace("fclose");
  119.                                     fclose(fp);
  120.                                     fp=NULL;
  121.                                     loaded=1;
  122.                                  }  else DisplayBeep(s);
  123.                               } else if(loaded) SetColors(&s->ViewPort);
  124.                               break;
  125.                      case 1:  if(loaded) {
  126.                                  SetRGB4(&s->ViewPort,0,0,0,0);
  127.                                  SetRGB4(&s->ViewPort,1,15,15,15);
  128.                               }
  129.                               if(name=FileRequest(w,SAVE)) {
  130.                                  if(fp=fopen(name,"w")) {
  131.                                     SetWait(w);
  132.                                     switch(SUBNUM(num)) {
  133.                                        case 0:  err=WriteWindow(fp,w,0,loaded);
  134.                                                 if(err) DisplayError(errortext[err]);
  135.                                                 break;
  136.                                        case 1:  err=WriteWindow(fp,w,1,loaded);
  137.                                                 if(err) DisplayError(errortext[err]);
  138.                                                 break;
  139.                                        case 2:  err=WriteWindow(fp,w,2,loaded);
  140.                                                 if(err) DisplayError(errortext[err]);
  141.                                                 break;
  142.                                     }
  143.                                     ClearWait(w);
  144.                                     fclose(fp);
  145.                                     fp=NULL;
  146.                                     if(err) unlink(name);
  147.                                  } else DisplayError("Cannot open file");
  148.                               } else if(loaded) SetColors(&s->ViewPort);
  149.                               break;
  150.                      case 2:  if(loaded) {
  151.                                  SetRGB4(&s->ViewPort,0,0,0,0);
  152.                                  SetRGB4(&s->ViewPort,1,15,15,15);
  153.                               }
  154.                               if(name=FileRequest(w,DELETE)) {
  155.                                  unlink(name);
  156.                               } else if(loaded) SetColors(&s->ViewPort);
  157.                               break;
  158.                      case 3:  break;
  159.                      case 4:  selected=ItemAddress(FIRSTMENU,(long)num)
  160.                                           ->Flags&CHECKED;
  161.                               ShowTitle(s,selected);
  162.                               break;
  163.                      case 5:  break;
  164.                      case 6:  ende=1;
  165.                               break;
  166.                   }
  167.                }
  168.                num=ItemAddress(FIRSTMENU,(long)num)->NextSelect;
  169.             }
  170.  
  171.             Trace("SetColors");
  172.             if(loaded) SetColors(&s->ViewPort);
  173.  
  174.          } else if(class==MENUVERIFY) {
  175.             if(loaded) {
  176.                SetRGB4(&s->ViewPort,0,0,0,0);
  177.                SetRGB4(&s->ViewPort,1,15,15,15);
  178.             }
  179.          }
  180.          Trace("Reply");
  181.       }
  182.    }
  183.    quit(NULL);
  184. }
  185. void _abort()
  186. {
  187.    quit(NULL);
  188. }
  189. void quit(char *err)
  190. {
  191.    if(err) DisplayError(err);
  192.    IFFCleanup();
  193.    ClearWait(NULL);
  194.    if(fp) fclose(fp);
  195.    if(w) CloseWindow(w);
  196.    if(s) CloseScreen(s);
  197.    FreeRequest();
  198.    if(ReqToolsBase) CloseLibrary((struct Library *)ReqToolsBase);
  199.    if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  200.    if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  201.    exit(0);
  202. }
  203.  
  204. void DisplayError(char *err)
  205. {
  206.    if(!IntuitionBase) return; /* Kein Alert möglich */
  207.    if(IntuitionBase->LibNode.lib_Version<36) {
  208.       char buffer[40];
  209.       short len;
  210.       buffer[0]=0;
  211.        strcpy(&buffer[1],"\240\30");
  212.       strcpy(&buffer[3],err);
  213.       len=strlen(err);
  214.       /* 2 Null-Bytes als Abschluß */
  215.       buffer[len+3+1]=0;
  216.       buffer[len+3+2]=0;
  217.       DisplayAlert(RECOVERY_ALERT,buffer,40L);
  218.    } else {
  219.       ULONG Flag=IDCMP_RAWKEY;
  220.       struct EasyStruct ES={
  221.          sizeof(struct EasyStruct),0,"IFFConvert 1.12",0,"OK"};
  222.       ES.es_TextFormat=(UBYTE *)err;
  223.  
  224.       EasyRequest(w,&ES,&Flag);
  225.    }
  226. }
  227.  
  228. void OpenDefaultScreen(void)
  229. {
  230.    if(w) {
  231.       ClearMenuStrip(w);
  232.       CloseWindow(w);
  233.    }
  234.    w=NULL;
  235.    if(s) CloseScreen(s);
  236.    s=NULL;
  237.    nw=new_window;
  238.    ns=NewScreen;
  239.    ns.Font=&txtattr;
  240.    if(!(s=OpenScreen(&ns)))
  241.       quit("FATAL ERROR: Cannot open default Screen");
  242.    nw.Screen=s;
  243.    if(!(w=OpenWindow(&nw)))
  244.    quit("FATAL ERROR: Cannot open default Window");
  245.    SetMenuStrip(w,FIRSTMENU);
  246.    ShowTitle(s,(long)selected);
  247. }
  248. void LoadScreen(FILE *fp)
  249. {
  250.    int err;
  251.  
  252.    ns=NewScreen;
  253.    nw=new_window;
  254.  
  255.    SetWait(w);
  256.    if(err=ReadPicSize(fp,&nw.Width,&nw.Height,
  257.                          &ns.Width,&ns.Height,&ns.Depth,&ns.ViewModes)) {
  258.       DisplayError(errortext[err]);
  259.       ClearWait(w);
  260.       return;
  261.    }
  262.    ClearWait(w);
  263.  
  264.    if(ns.Width<320) ns.Width=320;
  265.    if(ns.Height<200) ns.Height=200;
  266.    if(ns.Width<nw.Width) ns.Width=nw.Width;
  267.    if(ns.Height<nw.Height) ns.Height=nw.Height;
  268.  
  269.    ClearMenuStrip(w);
  270.    CloseWindow(w);
  271.    w=NULL;
  272.    CloseScreen(s);
  273.    s=NULL;
  274.    if(!(s=OpenScreen(&ns))) {
  275.       DisplayError("Cannot open screen");
  276.       OpenDefaultScreen();
  277.       return;
  278.    }
  279.    nw.Screen=s;
  280.  
  281.    if(!(w=OpenWindow(&nw))) {
  282.       DisplayError("Cannot open window");
  283.       OpenDefaultScreen();
  284.       return;
  285.    }
  286.  
  287.    SetMenuStrip(w,FIRSTMENU);
  288.    ShowTitle(s,(long)selected);
  289.    SetColors(&s->ViewPort);
  290.  
  291.    SetWait(w);
  292.    if(err=ReadBody(w->RPort,fp))
  293.         DisplayError(errortext[err]);
  294.  
  295.    ClearWait(w);
  296. }
  297.  
  298. #ifdef SAS_C
  299.  
  300. __chip USHORT Pointer[]= {
  301.    0x0000,0x0000,
  302.    0x0f80,0x0d80,
  303.    0x1040,0x1040,
  304.    0x2220,0x2020,
  305.    0x4210,0x4010,
  306.    0x8208,0x8008,
  307.    0x8208,0x8008,
  308.    0x8388,0x0000,
  309.    0x8008,0x8008,
  310.    0x8008,0x8008,
  311.    0x4010,0x4010,
  312.    0x2020,0x2020,
  313.    0x1040,0x1040,
  314.    0x0f80,0x0D80,
  315.    0x0000,0x0000
  316.    };
  317.  
  318. void SetWait(struct Window *w)
  319. {
  320.    SetPointer(w,Pointer,13L,16L,-6L,-6L);
  321. }
  322. void ClearWait(struct Window *w)
  323. {
  324.    if(w) ClearPointer(w);
  325. }
  326.  
  327. #else
  328.  
  329. USHORT Pointer[]= {
  330.    0x0000,0x0000,
  331.    0x0f80,0x0d80,
  332.    0x1040,0x1040,
  333.    0x2220,0x2020,
  334.    0x4210,0x4010,
  335.    0x8208,0x8008,
  336.    0x8208,0x8008,
  337.    0x8388,0x0000,
  338.    0x8008,0x8008,
  339.    0x8008,0x8008,
  340.    0x4010,0x4010,
  341.    0x2020,0x2020,
  342.    0x1040,0x1040,
  343.    0x0f80,0x0D80,
  344.    0x0000,0x0000
  345.    };
  346.  
  347. #define POINTERMEM (15L*4L)
  348. static USHORT *chipmem=0;
  349.  
  350. void SetWait(struct Window *w)
  351. {
  352.    short a;
  353.    ClearWait(w);
  354.    if(chipmem=AllocMem(POINTERMEM,MEMF_CHIP)) {
  355.       for(a=0;a<30;a++) chipmem[a]=Pointer[a];
  356.       SetPointer(w,chipmem,13L,16L,-6L,-6L);
  357.    }
  358. }
  359. void ClearWait(struct Window *w)
  360. {
  361.    if(w) ClearPointer(w);
  362.    if(chipmem) FreeMem(chipmem,POINTERMEM);
  363.    chipmem=0;
  364. }
  365.  
  366. #endif
  367.